package com.llb666.java.leetcode;

/**
 *
 * 荷兰国旗算法
 * Created by 斌~
 * 2021/4/14 11:32
 */
public class LeetCode75 {

    public static int[] sortColors(int[] nums) {
        for(int i = 0, j = 0, k = nums.length - 1;j<=k;++j){
            if(nums[j]==0){
                nums[j]=nums[i];
                nums[i++] = 0;
            }else if(nums[j]==2){
                nums[j--] =nums[k];
                nums[k--]=2;
            }
        }
        return nums;
    }

    public static void main(String[] args) {
        int[] nums = {1,2,0,1,2,1,0,1,2};
        int[] ints = sortColors(nums);
        for (int i =0; i<ints.length-1;i++ ){
            System.out.println(ints[i]);
        }
    }

}
